package com.lmz.order.service.impl;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmz.common.utils.PageUtils;
import com.lmz.common.utils.Query;

import com.lmz.order.dao.OrderItemDao;
import com.lmz.order.entity.OrderItemEntity;
import com.lmz.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    /**
     * queues：需要监听的所有队列
     * 参数：
     * 1.Message msg：原生消息的详细信息
     * 2.T<发送的消息的类型> 直接接收消息的类型
     */
    @RabbitHandler
    public void recieveMessage(Message msg, String message, Channel channel) {
        System.out.println(message);
        System.out.println(msg);
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        //手动确认应答，非批量
        try {
            channel.basicAck(deliveryTag, false);
            //拒绝确认
            /**
             * requeue：拒收后是否重新入队
             */
            //channel.basicNack(deliveryTag, false, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}